﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.DataStructure
{
    class SimpleArithmetic
    {
        #region 递归:1,1,2,3,5,8,13,21......
        private static int Fib(int i)
        {
            if (i <= 0)
            { return 0; }
            else if (i == 1)
            { return 1; }
            else
            {
                return Fib(i-1)+Fib(i-2);
            }
        }
        #endregion

        #region 三角形
        private static void Triangle()
        {
            int j = 7;
            for (int i = 0; i < 4; i++)
            {
                for (int w = 0; w <= i * 2; w++)
                {
                    Console.Write(" ");
                }
                int k = j;
                j -= 2;
                while (k > 0)
                {
                    Console.Write("*");
                    k--;
                }
                Console.WriteLine();
            }
        }
        #endregion
        
        #region 九九乘法
        private static void Mutiply()
        {
            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("{0}*{1}={2} \t",i,j,i*j);
                }
                Console.WriteLine();
            }
        }
        #endregion

        #region 获取字符串中出现次数最多的字母和个数
        private static void GetChar(string s)
        {
            char[] cArray = s.ToCharArray();
            Dictionary<char, int> dic = new Dictionary<char, int>();
            foreach (char c in cArray)
            {
                if (!dic.ContainsKey(c))
                {
                    dic.Add(c, 1);
                }
                else 
                {
                    dic[c]++;
                }
            }
            KeyValuePair<char,int> maxDic = dic.OrderBy(a => a.Value).Last();
            //int maxInt = dic.Max(a => a.Value);
        }
        #endregion

        #region 获取两个字符串的交集
        private static void AddString(string str1, string str2)
        {
            IEnumerable<char> newLst;
            List<char> LStr1 = new List<char>(str1.ToCharArray());
            List<char> LStr2 = new List<char>(str2.ToCharArray());
            LStr1.AddRange(LStr2);
            foreach (char c in LStr1.Distinct<char>())
            {
                Console.WriteLine(c.ToString());
            }
            Console.Read();
        }
        #endregion

        #region 最大公约数和最小公倍数
            private static void MaxComm()
        {
             int m, n, i, t;
            Console.WriteLine("请输入第一个整数：");
            m = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入第二个整数：");
            n = int.Parse( Console.ReadLine());
            if (m > n)
            {
                t = m;
                m = n;
                n = t;
            }
            for (i = m; i >= 1; i--)
            {
                if (m % i == 0 && n % i == 0)
                {
                    Console.WriteLine("最大公约数为：{0}",i);
                    break;
                }

            }
            for(i=n;i<=m*n;i++)
            {
                if(i%m==0&&i%n==0)
                {
                    Console.WriteLine(" 最小公倍数为：{0}",i);
                    break;
                }
            }
        }
        #endregion

        #region S=1*2+3*4+5*6……输出前20项的和S
            private static void sum1(int i)
            {
                int sum = 0;
                for (int j = 1; j <= i; j += 2)
                {
                    sum += j * (j + 1);
                }
                Console.WriteLine(sum);
            }
        #endregion

        static void Main()
        {
            //Triangle();
            //Mutiply();
            //GetChar("sssseeaaaaaaaaaaaaaaa");
            AddString("abc", "bcdef");
            //MaxComm();
            //sum1(40);
            Console.Read();
        }
    }
}
